#!/bin/bash

LT_version=1.0.0

# shellcheck source=/dev/null
source ./common.sh

########################## main ###############################
export NEWT_COLORS='
    root=,blue
    checkbox=,blue
    entry=,blue
    label=blue,
    actlistbox=,blue
    helpline=,blue
    roottext=,blue
    emptyscale=blue
    disabledentry=blue,
    '

main(){
    clear
    OPTION=$(whiptail --title "Linux Tools Version : $LT_version " --menu "
系统环境：${system_full_name}_${DEVICE_ARCH}  $PKG_MANAGER
系统时间：${date_time} ${time_zone}
请选择操作：" 26 60 15 \
    "net" "网络管理" \
    "sys" "系统管理" \
    "swm" "软件管理" \
    "app" "应用部署" \
    "disk" "磁盘管理" \
    "hdw"  "硬件管理" \
    "s"   "发送到服务器" \
    "i"   "安装到服务器" \
    "u"   "升级" \
    "q"   "退出" \
    3>&1 1>&2 2>&3)

    case "$OPTION" in
        sys )
            system
            ;;
        net )
            network
            ;;
        swm )
            software
            ;;
        app )
            application
            ;;
        disk )
            disk
            ;;
        hdw )
            hardware
            ;;
        s )
            transferSelf
            ;;
        i )
            installSelf
            ;;
        demo )
             execute_base_os demo
            ;;
        u )
            updateSelf
            ;;
        exit | quit | q )
            exit
            ;;
        esac
}

demo-ubuntu(){
    echo ">>>>>>>>>>>>> this is a demo function on ubuntu"
}
demo-centos(){
    echo ">>>>>>>>>>>>> this is a demo function on centos"
}

network() {
    clear
    OPTION=$(whiptail --title "Linux Tools Version : $LT_version " --menu "
系统环境：${system_full_name}_${DEVICE_ARCH}  $PKG_MANAGER
系统时间：${date_time} ${time_zone}
请选择操作【网络管理】：" 26 60 15 \
    "c" "网络配置" \
    "t" "网络测速" \
    "b" "返回主菜单" \
    "q" "退出" \
    3>&1 1>&2 2>&3)
    case "$OPTION" in
        t )
            echo "TODO"
            ;;
        c )
            networkCfg
            ;;
        b )
            main
            ;;
        q )
            exit
            ;;
    esac
}

system() {
    clear
    OPTION=$(whiptail --title "Linux Tools Version : $LT_version " --menu "
系统环境：${system_full_name}_${DEVICE_ARCH}  $PKG_MANAGER
系统时间：${date_time} ${time_zone}
请选择操作【系统管理】：" 26 60 15 \
    "l" "查看用户与组" \
    "a" "添加用户与组" \
    "d" "移除用户与组" \
    "t" "设置时间" \
    "n" "设置NTP服务器" \
    "b" "返回主菜单" \
    "q" "退出" \
    3>&1 1>&2 2>&3)
    case "$OPTION" in
        l | a | d | t | n )
            echo "TODO"
            ;;
        b )
            main
            ;;
        q )
            exit
            ;;
    esac
}

software() {
    clear
    OPTION=$(whiptail --title "Linux Tools Version : $LT_version " --menu "
系统环境：${system_full_name}_${DEVICE_ARCH}  $PKG_MANAGER
系统时间：${date_time} ${time_zone}
请选择操作【软件管理】：" 26 60 15 \
    "s" "更新国内源" \
    "i" "安装软件" \
    "f" "查找软件" \
    "u" "卸载软件" \
    "b" "返回主菜单" \
    "q" "退出" \
    3>&1 1>&2 2>&3)
    case "$OPTION" in
        s )
            chSource
            ;;
        i | f | u )
            echo "TODO"
            ;;
        b )
            main
            ;;
        q )
            exit
            ;;
    esac
}

application() {
    clear
    OPTION=$(whiptail --title "Linux Tools Version : $LT_version " --menu "
系统环境：${system_full_name}_${DEVICE_ARCH}  $PKG_MANAGER
系统时间：${date_time} ${time_zone}
请选择操作【应用部署】：" 26 60 15 \
    "docker" "安装Docker" \
    "k3s" "安装K3S" \
    "b" "返回主菜单" \
    "q" "退出" \
    3>&1 1>&2 2>&3)
    case "$OPTION" in
        docker )
            ./plugins/app-docker-installation.sh
            ;;
        k3s )
            echo "TODO"
            ;;
        b )
            main
            ;;
        q )
            exit
            ;;
    esac
}

disk() {
    clear
    OPTION=$(whiptail --title "Linux Tools Version : $LT_version " --menu "
系统环境：${system_full_name}_${DEVICE_ARCH}  $PKG_MANAGER
系统时间：${date_time} ${time_zone}
请选择操作【磁盘管理】：" 26 60 15 \
    "i" "查看磁盘列表" \
    "m" "挂载磁盘或分区" \
    "t" "硬盘检测" \
    "b" "返回主菜单" \
    "q" "退出" \
    3>&1 1>&2 2>&3)
    case "$OPTION" in
        i | m | t )
            echo "TODO"
            ;;
        b )
            main
            ;;
        q )
            exit
            ;;
    esac
}

hardware() {
    clear
    OPTION=$(whiptail --title "Linux Tools Version : $LT_version " --menu "
系统环境：${system_full_name}_${DEVICE_ARCH}  $PKG_MANAGER
系统时间：${date_time} ${time_zone}
请选择操作【硬件管理】：" 26 60 15 \
    "i" "网卡驱动" \
    "l" "硬件列表" \
    "b" "返回主菜单" \
    "q" "退出" \
    3>&1 1>&2 2>&3)
    case "$OPTION" in
        i | l )
            echo "TODO"
            ;;
        b )
            main
            ;;
        q )
            exit
            ;;
    esac
}

########################### functions ###########################


function updateSelf () {
    echo "======= 开始更新脚本 =========" 
    ##mv linux-tools.sh /usr/local/bin/linux-tools
    pkg_url="https://gitee.com/jurnlee/linux-tools/repository/archive/master.zip"
    git_url="https://gitee.com/jurnlee/linux-tools.git"
    if [ -x "$(command -v wget)" ]; then
        check_command unzip
        wget -O linux-tools.zip $pkg_url
        unzip -q linux-tools.zip -d ./linux-tools
        mv ./linux-tools/linux-tools-master/* ./linux-tools
        rm -rf ./linux-tools/linux-tools-master
    elif [ -x "$(command -v curl)" ]; then
        check_command unzip
        curl -o linux-tools.zip $pkg_url
        unzip -q linux-tools.zip -d ./linux-tools
        mv ./linux-tools/linux-tools-master/* ./linux-tools
        rm -rf ./linux-tools/linux-tools-master
    elif [ -x "$(command -v git)" ]; then
        git clone $git_url
    else
        echo "请安装wget/curl/git任一工具"
        back_exit
    fi
    cd linux-tools || output_error "下载linux-tools失败"
    chmod +x ./*.sh 
    echo "======= 更新完成 ========="  
    back_exit
}

function transferSelf0() {
    check_command scp
    echo "======= 开始传输脚本 =========" 
    ## 传输到指定服务器
    read -r -p "请输入目标服务器IP:端口：" ip
    ## 请输入目标服务器端口，不输入则默认22
    read -r -p "请输入目标服务器端口(不输入则默认22)：" port
    port=${port:-22}  
    read -r -p "请输入目标服务器账户：" user
    #read -r -p "请输入目标服务器密码：" passwd
    read -r -p "请输入目标服务器目录：" dir
    scp -P "$port" linux-tools.sh "$user"@"$ip":"$dir"/linux-tools/linux-tools.sh
    scp -P "$port" -r plugins/ "$user"@"$ip":"$dir"/linux-tools/
    echo "======= 传输完成 =========" 
    back_exit
}

function transferSelf() {
    check_command sshpass
    check_command scp
    check_command ssh
    # 提示用户输入目标服务器的IP地址
    read -r -p "请输入目标服务器的IP地址: " server_ip
    # 如果用户没有输入端口，则默认为22
    read -r -p "请输入目标服务器端口：" server_port
    server_port=${server_port:-22}
    # 提示用户输入目标目录
    read -r -p "请输入目标服务器上的目标目录: " target_dir
    # 提示用户输入用户名
    read -r -p "请输入用户名: " username
    # 提示用户输入密码
    read -r -s -p "请输入密码: " password
    echo

    # 检查目标目录是否存在，如果不存在则创建
    sshpass -p "$password" ssh -p "$server_port" "$username@$server_ip" "mkdir -p $target_dir"
    sshpass -p "$password" scp -P "$server_port" linux-tools.sh "$username@$server_ip:$target_dir"
    sshpass -p "$password" scp -P "$server_port" -r plugins "$username@$server_ip:$target_dir/plugins"

    echo "======= 传输完成 =========" 
    back_exit
}

function installSelf() {
    echo "TODO"
}

function installCnFont() {
    ./plugins/install-cn-font.sh
    main
}

function chSource() {
    ./plugins/change-mirrors.sh
    main
}

function networkCfg() {
    ./plugins/network-config.sh
    main
}

function installDocker() {
    ./plugins/install-docker.sh
    main
}

########################### main ###########################
main